﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Medical.Interstructrue.Base
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MedicalDbContext _medicalDbContext;

        public BaseRepository(MedicalDbContext medicalDbContext)
        {
            _medicalDbContext = medicalDbContext;
        }



        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Create(T entity)
        {
            try
            {
                _medicalDbContext.Add(entity);
                return _medicalDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Delete(T entity)
        {
            try
            {
                _medicalDbContext.Remove(entity);
                return _medicalDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> GetAll()
        {
            try
            {
                return _medicalDbContext.Set<T>().AsQueryable();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public T GetModel(int Id)
        {
            try
            {
                return _medicalDbContext.Set<T>().Find(Id);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        
        /// <summary>
        /// 异步查询单个实体
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<T> GetModelAsync(int Id)
        {
            try
            {
                return await _medicalDbContext.Set<T>().FindAsync(Id);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Update(T entity)
        {
            try
            {
                _medicalDbContext.Update(entity);
                return _medicalDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        /// <summary>
        /// 批量创建
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public int CreateRange(IEnumerable<T> entities)
        {
            try
            {
                _medicalDbContext.AddRange(entities);
                return _medicalDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public int UpdateRange(IEnumerable<T> entities)
        {
            try
            {
                _medicalDbContext.UpdateRange(entities);
                return _medicalDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
        /// <summary>
        /// long
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public T GetlongModel(long Id)
        {
            try
            {
                return _medicalDbContext.Set<T>().Find(Id);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                throw;
            }
        }
    }
}
